package com.cris.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cris.Common.CustomException;
import com.cris.Mapper.CategoryMapper;
import com.cris.Service.CategoryService;
import com.cris.Service.DishService;
import com.cris.Service.SetMealService;
import com.cris.entity.Category;
import com.cris.entity.Dish;
import com.cris.entity.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetMealService setMealService;

    public  void deleteById(Long id){
        //查询当前分类是否关联菜品,如果已经关联 抛出异常
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId,id);
        int count = dishService.count(lqw);
        if(count > 0){
            //已经关联 抛出异常
            throw new CustomException("当前分类下关联菜品不能删除");
        }
        //查询当前分类是否关联套餐,如果已经关联 抛出异常
        LambdaQueryWrapper<Setmeal> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Setmeal::getCategoryId,id);
        int count2 = setMealService.count(lqw2);
        if(count2 > 0){
            //已经关联 抛出异常
            throw new CustomException("当前分类下关联套餐不能删除");
        }

        //正常删除分类
        removeById(id);
    }
}
